package com.hgyc.mom.eam.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hgyc.mom.common.context.FileServiceContext;
import com.hgyc.mom.common.util.http.HttpUtils;
import com.hgyc.mom.common.vo.Result;
import com.hgyc.mom.core.mybatis.QueryWrapperUtil;
import com.hgyc.mom.eam.entity.EamEquipmentRepairAttachment;
import com.hgyc.mom.eam.service.EamEquipmentRepairAttachmentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * 设备维修附件Controller
 *
 * @author fwj
 * @since 2025-08-19
 */
@Tag(name = "设备维修附件控制器", description = "设备维修附件控制器")
@RestController
@RequestMapping("/eam/repair/attachment")
public class EamEquipmentRepairAttachmentController {
    @Autowired
    private EamEquipmentRepairAttachmentService eamEquipmentRepairAttachmentService;

    @Autowired
    private FileServiceContext fileServiceContext;

    @Operation(summary = "查询设备维修附件树列表", description = "查询设备维修附件树列表")
    @PostMapping("/list")
    public Result<List<EamEquipmentRepairAttachment>> pageList(@Parameter(description = "查询参数") @RequestBody EamEquipmentRepairAttachment eamEquipmentRepairAttachment) {
        QueryWrapper<EamEquipmentRepairAttachment> wrapper = QueryWrapperUtil.build(eamEquipmentRepairAttachment);
        List<EamEquipmentRepairAttachment> list = eamEquipmentRepairAttachmentService.list(wrapper);
        return Result.success(list);
    }

    @Operation(summary = "获取设备维修附件详细信息", description = "获取设备维修附件详细信息")
    @GetMapping(value = "/{id}")
    public Result<EamEquipmentRepairAttachment> getInfo(@Parameter(description = "设备维修附件ID") @PathVariable("id") Long id) {
        return Result.success(eamEquipmentRepairAttachmentService.getById(id));
    }

    /**
     * 通用上传请求（单个）
     */
    @PostMapping("/upload")
    public Result<String> uploadFile(@RequestParam("file") MultipartFile file) throws Exception
    {
        try
        {
            // 上传并返回新文件名称
            String filePath = fileServiceContext.uploadFile(file, "eam");

            return Result.success("上传成功！",filePath);
        }
        catch (Exception e)
        {
            return Result.error(e.getMessage());
        }
    }

    @Operation(summary = "删除附件", description = "删除附件")
    @DeleteMapping("/file")
    public Result<?> remove(@Parameter(description = "删除ID") @RequestParam("filePath") String filePath)
    {
        fileServiceContext.deleteFile(filePath);
        return Result.success();
    }

    @Operation(summary = "下载", description = "下载")
    @GetMapping("/download/{id}")
    public void download(@PathVariable("id") Long id, HttpServletResponse response) throws UnsupportedEncodingException {
        EamEquipmentRepairAttachment attachment = eamEquipmentRepairAttachmentService.getById(id);
        byte[] file = fileServiceContext.downloadFile(attachment.getFileUrl());
        String fileName = java.net.URLEncoder.encode(attachment.getFileName(), "UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename="+fileName);
        HttpUtils.writeData(response, file, "application/octet-stream");
    }
}
